home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWExcLib / Include / FWNewHel.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-08  |  6.1 KB  |  198 lines  |  [TEXT/MPS ]

  1. #ifndef FWNEWHEL_H
  2. #define FWNEWHEL_H
  3. //========================================================================================
  4. //
  5. //    File:                FWNewHel.h
  6. //    Release Version:    $ 1.0d11 $
  7. //
  8. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef   FWPRIDEB_H
  13. #include "FWPriDeb.h"
  14. #endif
  15.  
  16. #ifndef   FWAUTODE_H
  17. #include "FWAutoDe.h"
  18. #endif
  19.  
  20. #ifndef   FWEXCTAS_H
  21. #include "FWExcTas.h"
  22. #endif
  23.  
  24. #ifndef FWEXCDEF_H
  25. #include "FWExcDef.h"
  26. #endif
  27.  
  28. #ifdef FW_USE_NEW_HELPER
  29.  
  30. #if FW_LIB_EXPORT_PRAGMAS
  31. #pragma lib_export on
  32. #endif
  33.  
  34. //========================================================================================
  35. //    CLASS FW_CPrivSubHelper
  36. //========================================================================================
  37.  
  38. class FW_CPrivSubHelper
  39. {
  40. public:
  41.     
  42.     static void InitializeStorageArray(FW_SPrivExceptionGlobals& globals,
  43.                                     FW_CPrivSubHelper*array, int size);
  44.  
  45. private:
  46.  
  47.     friend class FW_CLASS_ATTR FW_CPrivNewHelper;
  48.         // FW_CPrivSubHelper should be a nested class of FW_CPrivNewHelper
  49.  
  50.     void* operator new(size_t size);
  51.     void  operator delete(void* object);
  52.     
  53.     FW_CPrivSubHelper(_FW_CAutoDestructObject *subObject, 
  54.                        size_t subObjectSize,
  55.                        FW_CPrivSubHelper    *nextSubHelper);
  56.     ~FW_CPrivSubHelper();
  57.     
  58.     _FW_CAutoDestructObject *fSubObject;
  59.         // The subobject being constructed.
  60.     
  61.     size_t fSubObjectSize;
  62.         // The size of the subobject being watched.
  63.         // This information is necessary for tracking nested subobjects.
  64.         
  65.     void *fSubObjectVTable;
  66.         // The objects vtable at time of last completed constructor
  67.         
  68.     FW_CPrivSubHelper    *fSiblings;
  69.         // Next link for subobject peer to this one.
  70. };
  71.  
  72. //========================================================================================
  73. //    CLASS FW_CPrivNewHelper
  74. //
  75. //         This helper object watches a dynamically allocated object until it is fully 
  76. //        constructed. If an exception is thrown before the object is fully constructed,
  77. //        this helper will delete the object.
  78. //========================================================================================
  79.  
  80. class FW_CPrivNewHelper FW_AUTO_DESTRUCT_OBJECT
  81. {
  82.  
  83. public:
  84.  
  85.     friend class FW_CLASS_ATTR _FW_CAutoDestructObject;
  86.  
  87.     enum
  88.     {
  89.         kMaxNewHelperStackItems = 100,
  90.             // Maximum items on NewHelper stack.
  91.             // This limits 'nested news', i.e. objects that are newed that have
  92.             //   constructors that new objects that have constructors that new objects...
  93.         kNewHelperStackSize = kMaxNewHelperStackItems*sizeof(FW_CPrivNewHelper*),
  94.             // New helper stack size in bytes.
  95.  
  96.         kSubHelperArraySize = 100,
  97.             // Maximum number of simultaneously tracked subobjects.
  98.             // Subobjects are only tracked until their parent object's constructor
  99.             //   has fully executed.
  100.         kSubHelperArrayByteSize = kSubHelperArraySize*sizeof(FW_CPrivSubHelper)
  101.             // Subobject helper array size in bytes.
  102.     };
  103.     
  104.     FW_CPrivNewHelper(__FW_OperatorNewHandler newHandler,
  105.                       __FW_OperatorDeleteHandler deleteHandler);
  106.     ~ FW_CPrivNewHelper();
  107.     
  108.     _FW_CAutoDestructObject *WatchObject(_FW_CAutoDestructObject *watchedObject,
  109.                                        size_t watchedSize) const;
  110.         // Watch watchedObject, return previous watched object.
  111.         // This method is not really const, 
  112.         // but it reduces unnecessary warnings to declare it so.
  113.     
  114.     _FW_CAutoDestructObject *ForgetObject() const;
  115.         // Stop watching any object.
  116.         // This method is not really const, 
  117.         // but it reduces unnecessary warnings to declare it so.
  118.     
  119.     void DeleteWatchedObject();
  120.         // Delete the watched object, including subojects of partially constructed class.
  121.     
  122.     void UpdateForEndConstructor(_FW_CAutoDestructObject *object, size_t size);
  123.         // This method is called only from the FW_END_CONSTRUCTOR macro.
  124.  
  125.     void PushNewHelper();
  126.         // Push this helper onto the helper stack.
  127.         
  128.     short IsWatching(_FW_CAutoDestructObject *object) const;
  129.         // Return 1 if this new helper is tracking object
  130.         
  131.     static short IsSubObject(void* subObject, void* object, size_t objectSize);
  132.  
  133.     static void Initialize(FW_SPrivExceptionGlobals& globals);
  134.         // Initialize the NewHelper and SubHelper modules
  135.         
  136.     static void Terminate();
  137.         // Terminate (cleanup) the NewHelper and SubHelper modules
  138.         
  139.     static FW_CPrivNewHelper *PopNewHelper();
  140.         // Pop top item from helper stack and return pointer to it.
  141.  
  142.     static FW_CPrivNewHelper *TopNewHelper();
  143.         // Return pointer to top item on helper stack, but don't pop.
  144.     
  145. private:
  146.  
  147.     FW_CPrivSubHelper* InList(_FW_CAutoDestructObject *subObject);
  148.         // Search list for entry tracking subObject.
  149.         // Returns NULL if no entry exists.
  150.  
  151.     _FW_CAutoDestructObject *fWatchedObject;
  152.         // The object being constructed.
  153.     
  154.     void *fObjectVTable;
  155.         // The objects v table at time of last completed constructor
  156.         
  157.     size_t fWatchedSize;
  158.         // The size of the object being watched.
  159.         // This information is necessary for tracking subobjects.
  160.         
  161.     FW_CPrivSubHelper    *fSubHelperList;
  162.         // First link for SubHelper tracking subojects nested in this object.
  163.         // Each subhelper tracks one subobject of the fWatchedObject.
  164.  
  165.     __FW_OperatorNewHandler    fNewHandler;
  166.         // The new handler to use to create memory block.
  167.         
  168.     __FW_OperatorDeleteHandler fDeleteHandler;
  169.         // The delete handler to use to delete the memory block if necessary.
  170. };
  171.  
  172. //----------------------------------------------------------------------------------------
  173. //    FW_CPrivNewHelper::IsSubObject
  174. //----------------------------------------------------------------------------------------
  175.  
  176. inline short FW_CPrivNewHelper::IsSubObject(void* subObject, void* object, size_t objectSize)
  177. {
  178.     return (subObject>object && (char*)subObject < (char*)object+objectSize);
  179. }
  180.     
  181. //----------------------------------------------------------------------------------------
  182. //    FW_CPrivNewHelper::IsWatching
  183. //----------------------------------------------------------------------------------------
  184.  
  185. inline short FW_CPrivNewHelper::IsWatching(_FW_CAutoDestructObject *object) const
  186. {
  187.     return (object>=fWatchedObject && (char*)object < (char*)fWatchedObject+fWatchedSize);
  188. }
  189.  
  190. #if FW_LIB_EXPORT_PRAGMAS
  191. #pragma lib_export off
  192. #endif
  193.  
  194.  
  195. #endif // FW_USE_NEW_HELPER
  196.  
  197. #endif
  198.